import type { PropType } from 'vue';
import { propTypes } from '@/utils/propTypes';
import type { PaginationProps } from './types/pagination';

import type { FetchSetting, BasicColumn, SorterResult, TableRowSelection, TableCustomRecord, SizeType } from './types/table';

// 平台默认设置
export const DLY_DEFAULT = {
    inset: true, //去掉表格和外层的间隔
    resizeHeightOffset: 2, //计算高度偏移量
    bordered: false, //默认显示斑马线
    paramsToPath: false, // 显示地址参数
};

export const basicProps = {
    clickToRowSelect: { type: Boolean, default: true },
    isTreeTable: Boolean,
    showTableSetting: Boolean,
    inset: { type: Boolean, default: DLY_DEFAULT.inset },
    sortFn: {
        type: Function as PropType<(sortInfo: SorterResult) => any>,
        default: (sortInfo: SorterResult) => {
            const { field, order } = sortInfo;
            if (field && order) {
                return {
                    // The sort field passed to the backend you
                    field,
                    // Sorting method passed to the background asc/desc
                    order,
                };
            } else {
                return {};
            }
        },
    },
    filterFn: {
        type: Function as PropType<(data: Partial<Recordable<string[]>>) => any>,
        default: (data: Partial<Recordable<string[]>>) => {
            return data;
        },
    },
    autoCreateKey: { type: Boolean, default: true },
    striped: { type: Boolean, default: false },
    showSummary: Boolean,
    summaryFunc: {
        type: [Function, Array] as PropType<(...arg: any[]) => any[]>,
        default: null,
    },
    summaryData: {
        type: Array as PropType<Recordable[]>,
        default: null,
    },
    indentSize: propTypes.number.def(24),
    canColDrag: { type: Boolean, default: true },
    api: {
        type: Function as PropType<(...arg: any[]) => Promise<any>>,
        default: null,
    },
    beforeFetch: {
        type: Function as PropType<Fn>,
        default: null,
    },
    afterFetch: {
        type: Function as PropType<Fn>,
        default: null,
    },
    formSubmit: {
        type: Function as PropType<Fn>,
        default: null,
    },
    handleSearchInfoFn: {
        type: Function as PropType<Fn>,
        default: null,
    },
    fetchSetting: {
        type: Object as PropType<FetchSetting>,
        default: () => {
            return {};
        },
    },
    // 立即请求接口
    immediate: { type: Boolean, default: true },
    paramsToPath: { type: Boolean, default: DLY_DEFAULT.paramsToPath },
    emptyDataIsShowTable: { type: Boolean, default: true },
    // 额外的请求参数
    searchInfo: {
        type: Object as PropType<Recordable>,
        default: null,
    },
    // 默认的排序参数
    defSort: {
        type: Object as PropType<Recordable>,
        default: null,
    },
    columns: {
        type: Array as PropType<BasicColumn[]>,
        default: () => [],
    },
    showIndexColumn: { type: Boolean, default: true },
    indexColumnProps: {
        type: Object as PropType<BasicColumn>,
        default: null,
    },
    actionColumn: {
        type: Object as PropType<BasicColumn>,
        default: null,
    },
    ellipsis: { type: Boolean, default: false },
    isCanResizeParent: { type: Boolean, default: false },
    canResize: { type: Boolean, default: true },
    clearSelectOnPageChange: propTypes.bool,
    resizeHeightOffset: propTypes.number.def(DLY_DEFAULT.resizeHeightOffset),
    rowSelection: {
        type: Object as PropType<TableRowSelection | null>,
        default: null,
    },
    title: {
        type: [String, Function] as PropType<string | ((data: Recordable) => string)>,
        default: null,
    },
    titleHelpMessage: {
        type: [String, Array] as PropType<string | string[]>,
    },
    maxHeight: propTypes.number,
    dataSource: {
        type: Array as PropType<Recordable[]>,
        default: null,
    },
    rowKey: {
        type: [String, Function] as PropType<string | ((record: Recordable) => string)>,
        default: 'id',
    },
    bordered: { type: Boolean, default: DLY_DEFAULT.bordered },
    pagination: {
        type: [Object, Boolean] as PropType<PaginationProps | boolean>,
        default: null,
    },
    loading: propTypes.bool,
    rowClassName: {
        type: Function as PropType<(record: TableCustomRecord<any>, index: number) => string>,
    },
    scroll: {
        type: Object as PropType<{ x: number | true; y: number }>,
        default: null,
    },
    beforeEditSubmit: {
        type: Function as PropType<
            (data: {
                record: Recordable;
                index: number;
                key: string | number;
                value: any;
            }) => Promise<any>
        >,
    },
    size: {
        type: String as PropType<SizeType>,
        default: 'middle',
    },
    showTopStat: {
        type: Boolean,
        default: false,
    },
    showDefStat: {
        type: Boolean,
        default: false,
    }
};